/*
* $Id: LocaleManager.java 268 2011-02-14 08:03:19Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
package apiary;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Vector;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Localization manager
 * @author lexus
 */
public class LocaleManager
{
    /** Instance class */
    private static LocaleManager instance = null;
    /**
     * Gets instance class
     */
    public static LocaleManager getInstance() {
        if(instance == null)
            instance = new LocaleManager();
        return instance;
    }

    /** Current locale */
    private String locale;
    /** Parser xml file of localization */
    private SAXParser saxParser;
    /** List localized strings */
    private Hashtable localeValues;

    /**
     * Container for localized strings
     * @author lexus
     */
    private class LocaleValue {
        /** locale */
        public String locale;
        /** value string */
        public String value;
    }

    /**
     * SAX handler.
     * @author lexus
     */
    private class LocaleHandler extends DefaultHandler
    {
        /** current processed tag */
        private String currentString;

        /**
         * Start xml tag
         */
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if(currentString != null){
                String value = attributes.getValue("v");
                setData(currentString, value, qName);
            }
            if(qName.equals("s"))
                currentString = attributes.getValue("n");
        }

        /**
         * End xml tag
         */
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if(qName.equals("s"))
                currentString = null;
        }
    }

    /**
     * Constructor
     */
    public LocaleManager() {
        try {
            //locale = System.getProperty("microedition.locale").substring(0, 2);
            locale = "ru";
            localeValues = new Hashtable();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            saxParser = factory.newSAXParser();
            InputStream file = getClass().getResourceAsStream("/res/locale_string.xml");
            saxParser.parse(file, new LocaleHandler());
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        } catch (ParserConfigurationException ex) {
            System.err.println(ex.getMessage());
        } catch (SAXException ex) {
            System.err.println(ex.getMessage());
        }
    }

    /**
     * Gets localized string
     * @param name name string
     * @return localized string.
     * If string doesn't exist, return null
     */
    public String getLocaleString(String name) {
        Vector localeVector = (Vector) localeValues.get(name);
        for(int i = 0; localeVector != null && i < localeVector.size(); i++) {
            LocaleValue localeValue = (LocaleValue) localeVector.elementAt(i);
            if(localeValue.locale.equals(locale))
                return localeValue.value;
        }
        return name;
    }

    /**
     * Sets data of string in memory
     * @param name name string
     * @param value value string
     * @param locale locale string
     */
    private void setData(String name, String value, String locale) {
        Vector localeVector = null;
        if(!localeValues.containsKey(name))
            localeValues.put(name, new Vector());
        localeVector = (Vector) localeValues.get(name);
        LocaleValue localeValue = new LocaleValue();
        localeValue.locale = locale;
        localeValue.value = value;
        localeVector.addElement(localeValue);
    }
}
